//
//  UIViewController+Extension.swift
//  LiveTrivia
//
//  Created by Gesen on 2017/11/12.
//  Copyright © 2017年 LiveTrivia. All rights reserved.
//

import UIKit
//import URLNavigator

extension UIViewController {
    
//    static var top: UIViewController? {
//        return topMost
//    }
}

extension UIViewController {
    
    func add(child controller: UIViewController, to container: UIView? = .none) {
        addChild(controller)
        (container ?? view)?.addSubview(controller.view)
        controller.view.frame = (container ?? view).bounds
        controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        controller.didMove(toParent: self)
    }
    
    func remove(child controller: UIViewController) {
        controller.willMove(toParent: nil)
        controller.view.removeFromSuperview()
        controller.removeFromParent()
    }
    
    func push(_ controller: UIViewController, animated: Bool = true) {
        if let navigation = self as? UINavigationController {
            navigation.pushViewController(controller, animated: animated)
            
        } else if let navigation = navigationController {
            navigation.pushViewController(controller, animated: animated)
            
        } else {
            present(controller, animated: animated)
        }
    }
    
    func showAlert(title: String,
                   message: String? = nil,
                   _ actions: [String] = [],
                   textField: ((UITextField) -> Void)? = nil,
                   action: ((Int) -> Void)? = nil) {
        guard !isBeingDismissed || !isBeingPresented else {
            return
        }
        
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert
        )
        
        for (index, title) in actions.enumerated() {
            let action = UIAlertAction(title: title, style: .default) { _ in
                action?(index)
            }
            alert.addAction(action)
        }
        
        if actions.isEmpty {
            alert.addAction(UIAlertAction(title: "好的", style: .cancel))
        }
        
        if let _ = textField {
            alert.addTextField(configurationHandler: textField)
        }
        
        present(alert, animated: true)
    }
    
    func showAlert(title: String,
                   message: String?,
                   _ actions: [String],
                   textField: ((UITextField) -> Void)?,
                   action: ((UIAlertController, Int) -> Void)?) {
        guard !isBeingDismissed || !isBeingPresented else {
            return
        }
        
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert
        )
        
        for (index, title) in actions.enumerated() {
            let action = UIAlertAction(title: title, style: .default) { _ in
                action?(alert, index)
            }
            alert.addAction(action)
        }
        
        if actions.isEmpty {
            alert.addAction(UIAlertAction(title: "好的", style: .cancel))
        }
        
        if let _ = textField {
            alert.addTextField(configurationHandler: textField)
        }
        
        present(alert, animated: true)
    }
    
    func showActionSheet(title: String? = nil,
                         message: String? = nil,
                         destructiveIndex: Int? = nil,
                         _ actions: [String],
                         _ actionHandler: ((Int) -> Void)?) {
        guard !isBeingDismissed || !isBeingPresented else {
            return
        }
        guard !actions.isEmpty else {
            return
        }
        
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .actionSheet
        )
        
        for (index, title) in actions.enumerated() {
            alert.addAction(UIAlertAction(
                title: title,
                style: index == destructiveIndex ? .destructive : .default,
                handler: { action in
                    actionHandler?(index)
                }
            ))
        }
        
        alert.addAction(UIAlertAction(title: "取消", style: .cancel))
        
        present(alert, animated: true)
    }
}

extension UIViewController {
    
    @IBAction func endEditing() {
        view.endEditing(true)
    }
    
    @IBAction func close() {
        view.endEditing(true)
        if
            let navigation = navigationController,
            navigation.viewControllers.first != self {
            guard let _ = presentedViewController else {
                navigation.popViewController(animated: true)
                return
            }
            dismiss(animated: true) {
                navigation.popViewController(animated: true)
            }
            
        } else {
            let presenting = presentingViewController ?? self
            presenting.dismiss(animated: true)
        }
    }
}
